package pl.soulmusic.phylactery.soap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.util.Log;

public class ServiceInvoker {
	public static final String TAG = "PhylacterySOAP";
	
	private static final String NAMESPACE = "service.soulmusic.pl"; //service package in reverse
	private static final String URL = 
		":8080/DataSyncService/services/SynchronizeData?wsdl";
	private static final String ADDPOI_METHOD_NAME = "addPOI";
	private static final String ADDTRACK_METHOD_NAME = "addTrack";
	private static final String SOAP_ACTION = "SynchronizeData";
	
	public static String gateway;
	
	public static final String SOAP_ACTION_OK = "Dodano";

	public static String addPOI(String login, String passwordHash, String name,
			String description, double latitude, double longitude,
			int maxDistance) {
		SoapObject request = new SoapObject(NAMESPACE, ADDPOI_METHOD_NAME);
		/*
		<element name="login" type="xsd:string"/>
		<element name="passwordHash" type="xsd:string"/>
		<element name="name" type="xsd:string"/>
		<element name="description" type="xsd:string"/>
		<element name="latitude" type="xsd:double"/>
		<element name="longitude" type="xsd:double"/>
		<element name="maxDistance" type="xsd:int"/>
		*/
		request.addProperty("login", login);
		request.addProperty("passwordHash", passwordHash);
		request.addProperty("name", name);
		request.addProperty("description", description);
		request.addProperty("latitude", new SoapPrimitive("class.xsd", "double", String.valueOf(latitude)));
		request.addProperty("longitude", new SoapPrimitive("class.xsd", "double", String.valueOf(longitude)));
		request.addProperty("maxDistance", new SoapPrimitive("class.xsd", "integer", String.valueOf(maxDistance)));

		return sendRequest(request);
	}
	
	public static String addTrack(String login, String passwordHash, double longitude,
			double latitude, String genreName, String albumName,
			String albumYear, String artistName, String artistCountry,
			String trackTitle, String trackYouUrl) {
		SoapObject request = new SoapObject(NAMESPACE, ADDTRACK_METHOD_NAME);
		/*
		<element name="login" type="xsd:string"/>
		<element name="passwordHash" type="xsd:string"/>
		<element name="longitude" type="xsd:double"/>
		<element name="latitude" type="xsd:double"/>
		<element name="genreName" type="xsd:string"/>
		<element name="albumName" type="xsd:string"/>
		<element name="albumYear" type="xsd:string"/>
		<element name="artistName" type="xsd:string"/>
		<element name="artistCountry" type="xsd:string"/>
		<element name="trackTitle" type="xsd:string"/>
		<element name="trackYouUrl" type="xsd:string"/>
		*/
		request.addProperty("login", login);
		request.addProperty("passwordHash", passwordHash);
		request.addProperty("longitude", new SoapPrimitive("class.xsd", "double", String.valueOf(longitude)));
		request.addProperty("latitude", new SoapPrimitive("class.xsd", "double", String.valueOf(latitude)));
		request.addProperty("genreName", genreName);
		request.addProperty("albumName", albumName);
		request.addProperty("albumYear", albumYear);
		request.addProperty("artistName", artistName);
		request.addProperty("artistCountry", artistCountry);
		request.addProperty("trackTitle", trackTitle);
		request.addProperty("trackYouUrl", trackYouUrl);

		return sendRequest(request);
	}	
	
	/**
	 * Sends SOAP request to given service
	 * @param request the request to be sent
	 * @return 0 if success, other values can have different meanings (i.e. -1 for error)
	 */
	protected static String sendRequest(SoapObject request) {
		SoapSerializationEnvelope envelope = 
				new SoapSerializationEnvelope(SoapEnvelope.VER11);

		envelope.setOutputSoapObject(request);
		String cUrl = "http://"+gateway+URL;
		HttpTransportSE androidHttpTransport = new HttpTransportSE(cUrl);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			if (result.getProperty(0).equals(SOAP_ACTION_OK)) {
				Log.i(TAG, "SOAP OK");
				return result.getProperty(0).toString();
			}
			else {
				Log.i(TAG, "SOAP FAULT");
				return result.getProperty(0).toString();
			}
		} catch (Exception e) {
			//e.printStackTrace();
			return e.toString();
		}
	}
}
